package com.jyb.util.capture;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.jyb.config.HttpCilentConfig;
import com.jyb.util.UrlUtil;
import com.jyb.vo.HgfxVo;

public class HgfxUtil {

	public static List<HgfxVo> capture(String tdh){
		
		List<HgfxVo> list=new ArrayList<HgfxVo>();
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response=null;
		try {
			
			String url="http://edi.easipass.com/dataportal/q.do?qn=dp_query_letpas";
			url=UrlUtil.hanadleUrl(url);
			
			HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
			response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			String str=EntityUtils.toString(entity);
			Document document=Jsoup.parse(str);
			String qid=document.select("input[name=qid]").get(0).val();
			url="http://edi.easipass.com/dataportal/query.do?entryid=&blno="+tdh+"&ctnno=&pagesize=30&qid="+qid+"";
			url=UrlUtil.hanadleUrl(url);
			
			HttpClientUtils.closeQuietly(response);
			
			httpGet = HttpCilentConfig.getHttpGet(url);
			response = httpclient.execute(httpGet);
			entity = response.getEntity();
			str=EntityUtils.toString(entity);
			document=Jsoup.parse(str);
			
			Elements tables=document.select("table[bordercolor=#7996c1][bgcolor=#FFFFFF]");
			if(tables.size()>0){
				Elements elements=document.select("table[bordercolor=#7996c1][bgcolor=#FFFFFF]").get(0).select("tr:gt(0)");
				for (Element element : elements) {
					Elements elementTds=element.select("td");
					
					HgfxVo hgfxVo=new HgfxVo();
					hgfxVo.setCm(elementTds.get(0).text());
					hgfxVo.setHc(elementTds.get(1).text());
					hgfxVo.setTdh(elementTds.get(2).text());
					hgfxVo.setXh(elementTds.get(3).text());
					hgfxVo.setBgdh(elementTds.get(4).text());
					hgfxVo.setZymt(elementTds.get(5).text());
					hgfxVo.setHgfxzt(elementTds.get(6).text());
					hgfxVo.setEDIZXJSSJ(elementTds.get(7).text());
					hgfxVo.setXlh(elementTds.get(8).text());
					
					list.add(hgfxVo);
				}
			}
			
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(response!=null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println(JSON.toJSONString(list));
		return list;
	}
	
}
